// store/user.ts
import { defineStore } from "pinia";
import piniaPersistConfig from "@/store/helper/persist";
export const useUserStore = defineStore('user', {
  state: () => ({
    isAuthenticated: false,
    decode: {} as unknown,
    userId: '111',
    avatar: 'https://t10.baidu.com/it/u=2841979291,228342409&fm=30&app=106&f=JPEG?w=640&h=640&s=FD8C38720772663559FCD5CA0000C0B1', // 用户头像 URL
    nowClass: 1 ,
    nowClassName: 'XXX' as any,
    identity: 'student',
    nowKnow:"XXX" as any
  }),
  getters: {
    getAuthenticated: (state) => state.isAuthenticated,
    getUser: (state) => state.decode,
    getUserId: (state) => state.userId,
    getNowClass: (state) => state.nowClass,
    getIdentity: (state) => state.identity,
    getAvatar: (state) => state.avatar,
    getNowKnow: (state) => state.nowKnow,
    getNowClassName: (state) => state.nowClassName
  },
  actions: {
    setAuth(isAuth: boolean) {
      if (isAuth) {
        this.isAuthenticated = isAuth;
      } else {
        this.isAuthenticated = false;
      }
    },
    setIdentity(identity: string) {
      if (identity) {
        this.identity = identity;
      } else {
        this.identity = "";
      }
    },
    setUser(decode: unknown) {
      if (decode) {
        this.decode = decode;
      } else {
        this.decode = {} as unknown;
      }
    },
    setUserId(userId: string) {
      if (userId) {
        this.userId = userId;
      } else {
        this.userId = '111';
      }
    },
    setAvatar(newAvatar: string) {
      if (newAvatar) {
        this.avatar = newAvatar;
      } else {
        this.avatar = 'https://t10.baidu.com/it/u=2841979291,228342409&fm=30&app=106&f=JPEG?w=640&h=640&s=FD8C38720772663559FCD5CA0000C0B1';
      }
    },
    setNowClass(nowClass: any) {
      if (nowClass) {
        this.nowClass = nowClass;
      } else {
        this.nowClass = 1;
      }
    },
    setNowKnow(nowKnow1: string) {
      if (nowKnow1) {
        this.nowKnow  = nowKnow1;
      } else {
        this.nowKnow = 'XXX';
      }
    },
    setNowClassName(nowClassName1: any) {
      if (nowClassName1) {
        this.nowClassName  = nowClassName1;
      } else {
        this.nowClassName = 'XXX';
      }
    }
  },
  persist: piniaPersistConfig("user")
});
